<?php
// PANDA POPULAR POSTS WIDGET v.1.0
// www.pandathemes.com
function popular_posts_Widget() {
	$settings = get_option("widget_popular_posts_widget");
	$title = $settings['title'];
	$number = $settings['number'];
	$thumbs = $settings['thumbs'];
	$thumbs_float = $settings['thumbs_float'];
	$width = $settings['width'];
	$height = $settings['height'];
	$titles = $settings['titles'];
	$size = $settings['size'];
	$comm = $settings['comm'];
	$excerpt = $settings['excerpt'];

	global $wpdb;
	$now = gmdate("Y-m-d H:i:s",time());
	$lastmonth = gmdate("Y-m-d H:i:s",gmmktime(date("H"), date("i"), date("s"), date("m")-12,date("d"),date("Y")));
	$popularposts = "SELECT ID, post_title, post_date, post_excerpt, comment_count, COUNT($wpdb->comments.comment_post_ID) AS 'stammy' FROM $wpdb->posts, $wpdb->comments WHERE comment_approved = '1' AND $wpdb->posts.ID=$wpdb->comments.comment_post_ID AND post_status = 'publish' AND post_date < '$now' AND post_date > '$lastmonth' AND comment_status = 'open' GROUP BY $wpdb->comments.comment_post_ID ORDER BY stammy DESC LIMIT ".$number;
	$posts = $wpdb->get_results($popularposts);
	$popular = '';

	if($posts){ ?>

<li>
<?php if ($title) { echo "<h3>".$title."</h3>" ;} ?>
<ul id="widget_posts" class="float_<?php echo $thumbs_float ?>">

	<?php foreach($posts as $post){ 
			$post_title = stripslashes($post->post_title);
			$permalink = get_permalink($post->ID);
			$exc = stripslashes($post->post_excerpt);
			$comments = stripslashes($post->comment_count);
			$image = get_post_meta($post->ID, "full_image_value", true);
			if(!$image){
				$image2 = get_template_directory_uri() .'/images/default.gif';
			}else{
				$image2 = get_template_directory_uri() .'/timthumb.php?src=' .$image. '&amp;w='.$width.'&amp;h='.$height.'&amp;zc=1&amp;q=90';
			}
			?>
			<li>
				<?php 
				if ($thumbs=="display") { ?><a href="<?php echo $permalink; ?>"><img src="<?php echo $image2 ?>" width="<?php echo $width ?>" height="<?php echo $height ?>" alt="<?php the_title(); ?>" /></a><?php ;}?>
				<?php // SELECT SIZE OF TITLE
					if ($titles=='display') {
						if ( ($size=="default") || (!($size<>"")) ) { ?><div class="f14 wt"><a href="<?php echo $permalink; ?>"><?php echo $post_title; ?></a></div><?php }
						elseif ($size > 17) { ?><h5 class="f<?php echo $size ?>"><a href="<?php echo $permalink; ?>"><?php echo $post_title; ?></a></h5><?php }
						else { ?><div class="f<?php echo $size ?> wt"><a href="<?php echo $permalink; ?>"><?php echo $post_title; ?></a></div><?php }
					}
				?>
				<?php if ($comm=='display') { ?>
					<span>
					<?php 
						$a = "<a href=\"";
						$b = "#comments\">";
						$d = "</a>";
						if ($comments == 1) {$c = " Comment";} else {$c = " Comments";};
						echo $a.$permalink.$b.$comments.$c.$d;
					?>
					</span>
				<?php ;} ?>
				<?php if ($excerpt=='display') { 
					echo $exc;
				;} ?>
				<div class="clear"><!-- --></div>
			</li>
		<?php } wp_reset_query(); ?>
</ul>
</li>
	<?php }
}
function popular_posts_WidgetAdmin() {
	$settings = get_option("widget_popular_posts_widget");
	if (isset($_POST['update_popular_posts_'])) {
		$settings['title'] = strip_tags(stripslashes($_POST['popular_posts_title']));
		$settings['number'] = strip_tags(stripslashes($_POST['popular_posts_number']));
		$settings['thumbs'] = strip_tags(stripslashes($_POST['popular_posts_thumbs']));
		$settings['thumbs_float'] = strip_tags(stripslashes($_POST['popular_posts_thumbs_float']));
		$settings['width'] = strip_tags(stripslashes($_POST['popular_posts_thumbs_width']));
		$settings['height'] = strip_tags(stripslashes($_POST['popular_posts_thumbs_height']));
		$settings['titles'] = strip_tags(stripslashes($_POST['popular_posts_titles']));
		$settings['size'] = strip_tags(stripslashes($_POST['popular_posts_size']));
		$settings['comm'] = strip_tags(stripslashes($_POST['popular_posts_comm']));
		$settings['excerpt'] = strip_tags(stripslashes($_POST['popular_posts_excerpt']));
		update_option("widget_popular_posts_widget",$settings); } ?>

	<p>
		Widget title:
		<input id="popular_posts_title" name="popular_posts_title" type="text" class="widefat" value="<?php echo $settings['title'] ?>" />
	</p>
	<p>
		Display up to:
		<input id="popular_posts_number" name="popular_posts_number" type="text" size="3" value="<?php echo $settings['number'] ?>" />
		posts
		<div style="height:12px; border-top:2px solid #DFDFDF;"><!-- --></div>
	</p>
	<p>
		Thumbs:
		<select name="popular_posts_thumbs">
			<option value="hide" <?php if ($settings["thumbs"]=="hide"):?> selected <?php endif; ?>>hide</option>
			<option value="display" <?php if ($settings["thumbs"]=="display"):?> selected <?php endif; ?>>display</option>
		</select>
		<select name="popular_posts_thumbs_float">
			<option value="left" <?php if ($settings["thumbs_float"]=="left"):?> selected <?php endif; ?>>Float: left</option>
			<option value="right" <?php if ($settings["thumbs_float"]=="right"):?> selected <?php endif; ?>>Float: right</option>
			<option value="none" <?php if ($settings["thumbs_float"]=="none"):?> selected <?php endif; ?>>Float: none</option>
		</select>
	</p>
	<p>
		Width:<input id="popular_posts_thumbs_width" name="popular_posts_thumbs_width" type="text" size="3" value="<?php if ($settings['width']<>"") { echo $settings['width'] ;} else { echo '70' ;} ?>" />
		Height:<input id="popular_posts_thumbs_height" name="popular_posts_thumbs_height" type="text" size="3" value="<?php if ($settings['height']<>"") { echo $settings['height'] ;} else { echo '50' ;} ?>" />
		<div style="height:12px; border-top:2px solid #DFDFDF;"><!-- --></div>
	</p>
	<p>
		Titles:
		<select name="popular_posts_titles">
			<option value="display" <?php if ($settings["titles"]=="display"):?> selected <?php endif; ?>>display</option>
			<option value="hide" <?php if ($settings["titles"]=="hide"):?> selected <?php endif; ?>>hide</option>
		</select>
		Size:
		<select name="popular_posts_size">
			<option value="default" <?php if ($settings["size"]=="default"):?> selected <?php endif; ?>>default</option>
			<option value="f12" <?php if ($settings["size"]=="f12"):?> selected <?php endif; ?>>12 px</option>
			<option value="f13" <?php if ($settings["size"]=="f13"):?> selected <?php endif; ?>>13 px</option>
			<option value="f14" <?php if ($settings["size"]=="f14"):?> selected <?php endif; ?>>14 px</option>
			<option value="f15" <?php if ($settings["size"]=="f15"):?> selected <?php endif; ?>>15 px</option>
			<option value="f16" <?php if ($settings["size"]=="f16"):?> selected <?php endif; ?>>16 px</option>
			<option value="f18" <?php if ($settings["size"]=="f18"):?> selected <?php endif; ?>>18 px</option>
			<option value="f20" <?php if ($settings["size"]=="f20"):?> selected <?php endif; ?>>20 px</option>
			<option value="f22" <?php if ($settings["size"]=="f22"):?> selected <?php endif; ?>>22 px</option>
			<option value="f24" <?php if ($settings["size"]=="f24"):?> selected <?php endif; ?>>24 px</option>
			<option value="f26" <?php if ($settings["size"]=="f26"):?> selected <?php endif; ?>>26 px</option>
			<option value="f30" <?php if ($settings["size"]=="f30"):?> selected <?php endif; ?>>30 px</option>
		</select>
	</p>
	<p>
		Comments counter:
		<select name="popular_posts_comm">
			<option value="hide" <?php if ($settings["comm"]=="hide"):?> selected <?php endif; ?>>hide</option>
			<option value="display" <?php if ($settings["comm"]=="display"):?> selected <?php endif; ?>>display</option>
		</select>
	</p>
<?php /*
	<p>
		Excerpt:
		<select name="popular_posts_excerpt">
			<option value="hide" <?php if ($settings["excerpt"]=="hide"):?> selected <?php endif; ?>>hide</option>
			<option value="display" <?php if ($settings["excerpt"]=="display"):?> selected <?php endif; ?>>display</option>
		</select>
	</p>
*/ ?>
	<input type="hidden" id="update_popular_posts_" name="update_popular_posts_" value="1" />

<?php }
register_sidebar_widget('Panda - Popular Posts v.1.1', 'popular_posts_Widget');
register_widget_control('Panda - Popular Posts v.1.1', 'popular_posts_WidgetAdmin', 200, 200);
?>